package com.ruoyi.monitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.ParentMonitorVo;
import com.ruoyi.monitor.domain.Monitor;
import com.ruoyi.monitor.service.IMonitorService;
import com.ruoyi.parentMonitor.domain.ParentMonitor;
import com.ruoyi.parentMonitor.service.ParentMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 班级监控Controller
 *
 * @author ruoyi
 * @date 2024-11-01
 */
@RestController
@RequestMapping("/monitor/monitor")
public class MonitorController extends BaseController {
    @Autowired
    private IMonitorService monitorService;
    @Autowired
    private ParentMonitorService parentMonitorService;

    /**
     * 查询班级监控列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:monitor:list')")
    @GetMapping("/list")
    public TableDataInfo list(Monitor monitor) {
        startPage();
        List<Monitor> list = monitorService.selectMonitorList(monitor);
        return getDataTable(list);
    }

    /**
     * 导出班级监控列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:monitor:export')")
    @Log(title = "班级监控", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Monitor monitor) {
        List<Monitor> list = monitorService.selectMonitorList(monitor);
        ExcelUtil<Monitor> util = new ExcelUtil<Monitor>(Monitor.class);
        util.exportExcel(response, list, "班级监控数据");
    }

    /**
     * 获取班级监控详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:monitor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(monitorService.selectMonitorById(id));
    }

    /**
     * 新增班级监控
     */
    @PreAuthorize("@ss.hasPermi('monitor:monitor:add')")
    @Log(title = "班级监控", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Monitor monitor) {
        return toAjax(monitorService.insertMonitor(monitor));
    }

    /**
     * 修改班级监控
     */
    @PreAuthorize("@ss.hasPermi('monitor:monitor:edit')")
    @Log(title = "班级监控", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Monitor monitor) {
        return toAjax(monitorService.updateMonitor(monitor));
    }

    /**
     * 删除班级监控
     */
    @PreAuthorize("@ss.hasPermi('monitor:monitor:remove')")
    @Log(title = "班级监控", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(monitorService.deleteMonitorByIds(ids));
    }

    /**
     * 新增家长绑定
     */
    @PreAuthorize("@ss.hasPermi('monitor:monitor:bind')")
    @PostMapping("/bind")
    public AjaxResult bind(@RequestBody ParentMonitor parentMonitor) {
        try {
            return toAjax(parentMonitorService.save(parentMonitor));
        } catch (Exception e) {
            throw new RuntimeException("请勿重复绑定");
        }
    }

    /**
     * 监控报修
     */
    @PostMapping("/repair")
    public AjaxResult repair(@RequestBody Monitor monitor) {
        return toAjax(monitorService.repair(monitor));
    }

    @GetMapping("/listByParent")
    public AjaxResult getListByParent(ParentMonitorVo parentMonitorVo) {
        System.out.println(parentMonitorVo);
        Monitor monitor = new Monitor();
        monitor.setId(parentMonitorVo.getId());
        monitor.setSchoolName(parentMonitorVo.getSchoolName());
        monitor.setClassName(parentMonitorVo.getClassName());
        List<Long> idList = parentMonitorService.list(new LambdaQueryWrapper<ParentMonitor>()
                        .eq(ParentMonitor::getParentId, parentMonitorVo.getParentId())).stream()
                .map(ParentMonitor::getMonitorId).toList();

        List<Monitor> reList = monitorService.selectMonitorList(monitor);
        reList.removeIf(m -> !idList.contains(m.getId()));

        return AjaxResult.success(reList);
    }
}
